{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GeoJSON导入File GDB\n",
    "python版本：2.7\n",
    "\n",
    "核心第三方包：arcpy & geopandas\n",
    "\n",
    "数据：\n",
    "- [省份全拼.json](./json/): 省内行政区划（到市一级），数据来源于网络，2019年9月14日访问之前获取数据链接已经不存在\n",
    "\n",
    "导入方式：使用GeoPandas读取GeoJSON，转换为ShapeFile，再使用ArcPy将ShapeFile导入GDB中。\n",
    "\n",
    "PS：在ArcMap中导入JSON的工具只支持符合ArcGIS REST API所定义结构的JSON数据，在ArcGIS Pro中支持GeoJSON。就该练习提供的数据，使用ArcGIS Pro进行转化依然会报错，原因是要素有数组类型的属性值。\n",
    "\n",
    "PPS：GDAL支持FileGDB的读写，需要安装Esri提供的一个驱动，比较麻烦。感兴趣可以研究一下，链接:https://gdal.org/drivers/vector/filegdb.html\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import geopandas as gpd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import arcpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "scratch = \"./scratch\"\n",
    "if os.path.exists(scratch):\n",
    "    os.rmdir(scratch)\n",
    "os.makedirs(scratch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists(\"./boarders_nb.gdb\"):\n",
    "    arcpy.CreateFileGDB_management(\"./\", \"boarders_nb.gdb\")\n",
    "arcpy.env.workspace = \"./boarders_nb.gdb\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "arcpy.env.overwriteOutput = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "anhui.json Processed\n",
      "beijing.json Processed\n",
      "china.json Processed\n",
      "chongqing.json Processed\n",
      "fujian.json Processed\n",
      "gansu.json Processed\n",
      "guangdong.json Processed\n",
      "guangxi.json Processed\n",
      "guizhou.json Processed\n",
      "hainan.json Processed\n",
      "hebei.json Processed\n",
      "heilongjiang.json Processed\n",
      "henan.json Processed\n",
      "hongkong.json Processed\n",
      "hubei.json Processed\n",
      "hunan.json Processed\n",
      "jiangsu.json Processed\n",
      "jiangxi.json Processed\n",
      "jilin.json Processed\n",
      "liaoning.json Processed\n",
      "macau.json Processed\n",
      "neimenggu.json Processed\n",
      "ningxia.json Processed\n",
      "qinghai.json Processed\n",
      "shan1xi.json Processed\n",
      "shan3xi.json Processed\n",
      "shandong.json Processed\n",
      "shanghai.json Processed\n",
      "sichuan.json Processed\n",
      "taiwan.json Processed\n",
      "tianjin.json Processed\n",
      "xinjiang.json Processed\n",
      "xizang.json Processed\n",
      "yunnan.json Processed\n",
      "zhejiang.json Processed\n"
     ]
    }
   ],
   "source": [
    "for filename in os.listdir(\"./json/\"):\n",
    "    if arcpy.Exists(filename[:-5]):\n",
    "        arcpy.Delete_management(filename[:-5])\n",
    "    if filename.endswith(\".json\"):\n",
    "        file = \"./json/{0}\".format(filename)\n",
    "        df = gpd.read_file(file, encoding = 'utf-8')\n",
    "        df.to_file(u\"{0}/temp.shp\".format(scratch), encoding=\"utf-8\")\n",
    "        # df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')\n",
    "        arcpy.CopyFeatures_management(\"{0}/temp.shp\".format(scratch),filename[:-5])\n",
    "        arcpy.Delete_management(\"{0}/temp.shp\".format(scratch))\n",
    "        print(\"{0} Processed\".format(filename))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "if os.path.exists(scratch):\n",
    "    os.rmdir(scratch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[u'anhui',\n",
       " u'beijing',\n",
       " u'china',\n",
       " u'chongqing',\n",
       " u'fujian',\n",
       " u'gansu',\n",
       " u'guangdong',\n",
       " u'guangxi',\n",
       " u'guizhou',\n",
       " u'hainan',\n",
       " u'hebei',\n",
       " u'heilongjiang',\n",
       " u'henan',\n",
       " u'hongkong',\n",
       " u'hubei',\n",
       " u'hunan',\n",
       " u'jiangsu',\n",
       " u'jiangxi',\n",
       " u'jilin',\n",
       " u'liaoning',\n",
       " u'macau',\n",
       " u'neimenggu',\n",
       " u'ningxia',\n",
       " u'qinghai',\n",
       " u'shan1xi',\n",
       " u'shan3xi',\n",
       " u'shandong',\n",
       " u'shanghai',\n",
       " u'sichuan',\n",
       " u'taiwan',\n",
       " u'tianjin',\n",
       " u'xinjiang',\n",
       " u'xizang',\n",
       " u'yunnan',\n",
       " u'zhejiang']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arcpy.ListFeatureClasses()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:arcpy10-7] *",
   "language": "python",
   "name": "conda-env-arcpy10-7-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
